class BlackDeathPlayerController extends GamePlayerController
	dependson(BlackDeathPawn);

var BD_UseableObject SightedUseableObject;

simulated event PostBeginPlay()
{
	super.PostBeginPlay();
	UpdateUseable();
	setTimer(0.2, true, 'UpdateUseable');//continuoucsly check for new useables
}

function UpdateUseable()
{
	if(BlackDeathHeroPawn(Pawn) != none)
	{
		SightedUseableObject = getSightedUseable();
		WorldInfo.Game.Broadcast(None, SightedUseableObject);//`log("SightedUseable is: "@SightedUseableObject);
	}
}

function bool PerformedUseAction()
{
	if( WorldInfo.Pauser == PlayerReplicationInfo )
	{
		if( Role == Role_Authority )
		{
			// unpause and move on
			SetPause( false );
		}
		return true;
	}

    	if ( Pawn == None )
	{
		return true;
	}

	if( Role < Role_Authority )
	{
		return false;
	}

	if( Vehicle(Pawn) != None )
	{
		return Vehicle(Pawn).DriverLeave(false);
	}

	if(SightedUseableObject != none)
	{
		return SightedUseableObject.use(pawn);
	}

	return TriggerInteracted();
}

//Check for and return the "best" (one we're are most faceing) useable object from the pawn
function BD_UseableObject getSightedUseable()
{
	local BD_UseableObject O, Best;
	local vector ViewDir, PawnLoc2D, OLoc2D;
	local float NewDot, BestDot;

	PawnLoc2D = Pawn.Location;
	PawnLoc2D.Z = 0;
	ViewDir = vector(Pawn.Rotation);

	ForEach Pawn.OverlappingActors(class'BD_UseableObject', O, BlackDeathHeroPawn(Pawn).UseableCheckRadius)
	{
		OLoc2D = O.Location;
		Oloc2D.Z = 0;
		NewDot = Normal(OLoc2D-PawnLoc2D) Dot ViewDir;
		if ( ((Best == None) || (NewDot > BestDot)) && Newdot >= 0.5 )
		{
			// check that object is visible
			if ( FastTrace(O.Location,Pawn.Location) )
			{
				Best = O;
				BestDot = NewDot;
			}
		}
	}
	return Best;
}
	
exec function Run()
{
	if(BlackDeathHeroPawn(Pawn) != none)
	{
		BlackDeathHeroPawn(Pawn).StartRun();
	}
}

exec function EndRun()
{
	if(BlackDeathHeroPawn(Pawn) != none)
	{
		BlackDeathHeroPawn(Pawn).EndRun();
	}
}	

exec function ToggleAim()
{
	if(BlackDeathHeroPawn(Pawn) != none)
	{
		BlackDeathHeroPawn(Pawn).ToggleAim();
	}
}

exec function ToggleCam()
{
	if(BlackDeathCamera(PlayerCamera).CameraStyle == 'ThirdPerson')
	{
		BlackDeathCamera(PlayerCamera).ToggleShoulder();
	}
}

exec function PauseMenuEvent()
{
       local int i;
       local Sequence GameSeq;
       local array<SequenceObject> AllSeqEvents;
 
       GameSeq = WorldInfo.GetGameSequence();
 
       if(GameSeq != None)
       {
               GameSeq.FindSeqObjectsByClass(class'SeqEvent_PauseMenuEvent', true, AllSeqEvents);
               for(i=0; i<AllSeqEvents.Length; i++)
                       SeqEvent_PauseMenuEvent(AllSeqEvents[i]).CheckActivate(WorldInfo, None);
       }
       ConsoleCommand("pause");
}	


defaultproperties
{
	DesiredFOV=90.000000
	CameraClass=class'BlackDeath.BlackDeathCamera'
	InputClass=class'BlackDeath.BlackDeathInput'
}


